import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.util.Vector;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;


public class mainFrame extends JFrame{
	
	// GUI
	private JSplitPane splitPane;
	private JPanel controlPanel;
	private JButton reservationButton;
	private JButton signinButton;
	private JButton checkButton;
	private JButton roomManagementButton;
	private JButton memberButton;
	private JButton checkoutButton;
	private JButton settingButton;
	private JButton onlineHelpButton;
	private JButton signoutButton;
	private roomLayoutPanel roomPanel;
	private JTextField stateTextField;
	
	// handlers
	private WindowClosingHandler windowClosingHandler;
	private ReservationHandler reservationHandler;
	private SignInHandler signInHandler;
	private CheckHandler checkHandler;
	private RoomManagementHandler roomManagementHandler;
	private MemberHandler memberHandler;
	private CheckoutHandler checkoutHandler;
	private EmployeeHandler employeeHandler;
	private HelpHandler helpHandler;
	private SignoutHandler signoutHandler;
	
	// data controls
	private roomManager rm;
	private memberManager mm;
	private UserManager um;
	
	public mainFrame(){
		initWindow();
		initData();
		layoutGUI();
	}

	public mainFrame(UserManager initUm)
	{
		initWindow();
		um = initUm;
		initData();
		layoutGUI();
	}
	
	private void layoutGUI() {
		// TODO Auto-generated method stub
		controlPanel = new JPanel();
		reservationButton = new JButton("Reservation");
		signinButton = new JButton("Sign in");
		checkButton = new JButton("Check");
		roomManagementButton = new JButton("Room Management");
		memberButton = new JButton("Membership Management");
		checkoutButton = new JButton("Checkout");
		settingButton = new JButton("Setting");
		onlineHelpButton = new JButton("Help");
		signoutButton = new JButton("Sign out");
		stateTextField = new JTextField();
		stateTextField.setText("Welcome  " + um.getCurrentUser().getName());
		stateTextField.setEditable(false);
		controlPanel.setLayout(new GridLayout(10,1));
		controlPanel.add(reservationButton);
		controlPanel.add(signinButton);
		controlPanel.add(checkButton);
		controlPanel.add(roomManagementButton);
		controlPanel.add(memberButton);
		controlPanel.add(checkoutButton);
		controlPanel.add(settingButton);
		controlPanel.add(onlineHelpButton);
		controlPanel.add(signoutButton);
		controlPanel.add(stateTextField);
		roomPanel = new roomLayoutPanel(rm);
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setDividerLocation(200);
		splitPane.add(controlPanel);
		splitPane.add(roomPanel);
		this.add(splitPane);
		
		// add handlers
		reservationHandler = new ReservationHandler();
		roomManagementHandler = new RoomManagementHandler();
		signInHandler = new SignInHandler();
		checkHandler = new CheckHandler();
		memberHandler = new MemberHandler();
		checkoutHandler = new CheckoutHandler();
		employeeHandler = new EmployeeHandler();
		helpHandler = new HelpHandler();
		signoutHandler = new SignoutHandler();
		
		reservationButton.addActionListener(reservationHandler);
		signinButton.addActionListener(signInHandler);
		checkButton.addActionListener(checkHandler);
		roomManagementButton.addActionListener(roomManagementHandler);
		memberButton.addActionListener(memberHandler);
		checkoutButton.addActionListener(checkoutHandler);
		settingButton.addActionListener(employeeHandler);
		onlineHelpButton.addActionListener(helpHandler);
		signoutButton.addActionListener(signoutHandler);
		
		// size and locate the dialog at the center of the screen
		this.pack();
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
	}

	// initialize the data
	private void initData() {
		// TODO Auto-generated method stub
		rm = new roomManager();
		mm = new memberManager();
	}
	
	// initialize the window 
	private void initWindow() {
		// TODO Auto-generated method stub
		this.setTitle("KTV Room Management System");
		windowClosingHandler = new WindowClosingHandler();
		this.addWindowListener(windowClosingHandler);
		this.setResizable(false);
	}
	
	
	public static void main (String[] args){
		mainFrame temp = new mainFrame();
		temp.setVisible(true);
	}
	
	class WindowClosingHandler implements WindowListener{
		@Override
		public void windowActivated(WindowEvent arg0) {}
		public void windowClosed(WindowEvent arg0) {}
		public void windowClosing(WindowEvent arg0) {
			// TODO Auto-generated method stub
			int ans = JOptionPane.showConfirmDialog(mainFrame.this, 
					"Are you sure to sign out? ", "Sign out", JOptionPane.YES_NO_OPTION);
			if (ans == JOptionPane.YES_OPTION){
				um.updateUser(um.getCurrentUser());
				System.exit(0);
				}
		}

		@Override
		public void windowDeactivated(WindowEvent arg0) {}
		public void windowDeiconified(WindowEvent arg0) {}
		public void windowIconified(WindowEvent arg0) {}
		public void windowOpened(WindowEvent arg0) {}
	}
	
	class ReservationHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("Start Reservation");
			reservationDialog temp = new reservationDialog(rm,mainFrame.this);
			temp.setVisible(true);
		}
		
	}
	
	class RoomManagementHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("Start Room Management");
			roomManagementDialog temp = new roomManagementDialog(rm);
			temp.setVisible(true);
		}
		
	}
	
	class MemberHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("Start Membership Management");
			mm.loadMembers();
			membershipFrame temp = new membershipFrame(mm);
			temp.setVisible(true);
		}
		
	}
	
	class CheckoutHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("Start Check Out");
			checkoutDialog temp = new checkoutDialog(rm);
			temp.setVisible(true);
		}
		
	}
	
	class SignoutHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("Start Sign Out");
			int ans = JOptionPane.showConfirmDialog(mainFrame.this, 
					"Are you sure to sign out? ", "Sign out", JOptionPane.YES_NO_OPTION);
			if (ans == JOptionPane.YES_OPTION){
				um.updateUser(um.getCurrentUser());
				System.exit(0);
				
				}
					
		}
		
	}
	
	class EmployeeHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			SettingFrame temp = new SettingFrame(mainFrame.this,um);
			temp.setVisible(true);
		}
		
	}
	
	class HelpHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			//helpFrame temp = new helpFrame();
			//temp.setVisible(true);
			
			File helpFile = new File("help.txt");
			try                                      //try statement
	        {
				Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + helpFile.getPath());

	        } 
	        catch (Exception e)                    //catch any exceptions here
	        {
	        	System.out.println("Error" + e );  //print the error
	        }
		
		}
		
	}
	
	class SignInHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			Icon icon = null;
			Integer[] rooms = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
			int roomNum = (int) JOptionPane.showInputDialog(
					mainFrame.this, "Destination: ", 
					"Sign in", JOptionPane.PLAIN_MESSAGE, 
					icon, rooms, rooms[0]);
			if (!rm.getCurrentHost(roomNum).getName().equals(""))
				JOptionPane.showMessageDialog(mainFrame.this, "Error: The room is occupied by others now.", "Error", JOptionPane.ERROR_MESSAGE);
			else {
				Vector<host> hosts = rm.getReservations(roomNum);
				String name = JOptionPane.showInputDialog(mainFrame.this, "Host name: ", "Sign In", JOptionPane.PLAIN_MESSAGE);
				host h = new host("", -1, -1, -1, "", "", "", "", -1);
				boolean have = false;
				for (int i=0; i<hosts.size(); i++){
					if (hosts.get(i).getName().equals(name)){
						have = true;
						h = hosts.get(i);
					}		
				}
				if (have){
					if (rm.signin(h)){
						JOptionPane.showMessageDialog(mainFrame.this, "Sign In Successfully!");
						JOptionPane.showMessageDialog(mainFrame.this, rm.toString(rm.getRoomRects()[roomNum-1].getRoomInfo()));
					}
					else JOptionPane.showMessageDialog(mainFrame.this, "Sorry, you have to wait for a while");
				}
			}
		}
		
	}
	
	class CheckHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			checkRooms temp = new checkRooms(rm);
		}
		
	}
}

